<template>
  <div class="sign-body">
    <div class="sign-image">
      <el-avatar size="large" :src="imageUrl"></el-avatar>
    </div>
    <div class="sign-text">{{ nickName }}</div>
    <!-- <div class="sign-label">
      当前矿石数量：<el-tag size="mini" type="success">{{ currentPoint }}</el-tag>
    </div> -->
    <!-- <div class="sign-label">
      连续签到天数：<el-tag size="mini" type="success">{{ continueSignDays }}</el-tag>
    </div> -->
    <div class="sign-btn" v-if="!loading">
      <el-button v-if="!login" type="primary" @click="toLogin">去登录</el-button>
      <el-button v-else type="primary" :loading="signing" :disabled="todaySign" @click="toSign">{{ todaySign ? '已签到' : '去签到' }}</el-button>
    </div>
    <!-- {{ store }} -->
  </div>
</template>

<script>
// import { getUserInfo } from './utils.js';
export default {
  data() {
    return {
      // 头像
      imageUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
      // 昵称
      nickName: 'null',
      // 当前矿石数量
      currentPoint: 0,
      // 连续签到天数
      continueSignDays: 0,
      // 是否登录
      login: false,
      // 今日是否已签到
      todaySign: true,
      loading: true,
      signing: false,
      store: this.$store.getters.foo,
    };
  },
  async mounted() {
    this.loading = true;
    // console.log(getUserInfo());
    // 获取用户信息
    let resp = await this.userInfo();
    console.log(resp);
    // 判断cookie有效性
    this.login = !resp.err_no && resp.data;
    if (!this.login) {
      this.loading = false;
      return;
    }
    // 头像，昵称
    this.imageUrl = resp.data.avatar_large;
    this.nickName = resp.data.user_name;
    // 矿石数量
    // resp = await this.sumpoint();
    // this.currentPoint = resp.data.sum_point;
    // 连续签到天数
    // resp = await getSignData();
    // this.continueSignDays = resp.data.data.cont_count;
    // 当前签到状况
    resp = await this.todayStatus();
    this.todaySign = resp.data;
    this.loading = false;
  },
  methods: {
    userInfo() {
      return fetch('https://api.juejin.cn/user_api/v1/user/get', {
        headers: {
          cookie: document.cookie,
        },
        method: 'GET',
        credentials: 'include',
      }).then(res => res.json());
    },
    sumpoint() {
      return fetch('https://api.juejin.cn/growth_api/v1/check_in', {
        headers: {
          cookie: document.cookie,
        },
        method: 'POST',
        credentials: 'include',
      }).then(res => res.json());
    },
    todayStatus() {
      return fetch('https://api.juejin.cn/growth_api/v1/get_today_status', {
        headers: {
          cookie: document.cookie,
        },
        method: 'GET',
        credentials: 'include',
      }).then(res => res.json());
    },
  },
};
</script>
